package com.iotechn.unimall.admin.api.warehouseLossOperLog;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.WarehouseLossOperLogDO;
import com.iotechn.unimall.data.mapper.WarehouseLossOperLogMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class WarehouseLossOperLogServiceImpl implements WarehouseLossOperLogService {
    @Autowired
    private WarehouseLossOperLogMapper warehouseLossOperLogMapper;

    @Override
    public Page<WarehouseLossOperLogDO> list(Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<WarehouseLossOperLogDO> wrapper = new EntityWrapper<WarehouseLossOperLogDO>();
        wrapper.orderBy("id", false);
        Integer count = warehouseLossOperLogMapper.selectCount(wrapper);
        List<WarehouseLossOperLogDO> WarehouseLossOperLogDOS = warehouseLossOperLogMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(WarehouseLossOperLogDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WarehouseLossOperLogDO create(WarehouseLossOperLogDO warehouseLossOperLogDO, Long adminId) throws ServiceException {
        Date now = new Date();
        warehouseLossOperLogDO.setGmtCreate(now);
        warehouseLossOperLogDO.setGmtUpdate(now);
        if (warehouseLossOperLogMapper.insert(warehouseLossOperLogDO) > 0) {
            warehouseLossOperLogDO.setId(warehouseLossOperLogDO.getId());
            return warehouseLossOperLogDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(WarehouseLossOperLogDO warehouseLossOperLogDO, Long adminId) throws ServiceException {
        Long id = warehouseLossOperLogDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        warehouseLossOperLogDO.setGmtUpdate(now);
        if (warehouseLossOperLogMapper.updateById(warehouseLossOperLogDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (warehouseLossOperLogMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public WarehouseLossOperLogDO detail(Long id, Long adminId) throws ServiceException {
        return warehouseLossOperLogMapper.selectById(id);
    }

    @Override
    public List<WarehouseLossOperLogDO> listAll() throws ServiceException {
        return warehouseLossOperLogMapper.selectList(new EntityWrapper<WarehouseLossOperLogDO>());
    }

}